using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Data;

namespace DAO
{
    public class DataProvider
    {
        private  readonly SqlConnection Connection;
        private  readonly SqlCommand Command;

        public DataProvider()
        {
            Connection = new SqlConnection("Data Source=NHUYTAN-PC;Initial Catalog=DMS;Integrated Security=True");
            Command = new SqlCommand();
  
        }

        public void Open()
        {
            try
            {
                if (Connection.State == ConnectionState.Open)
                {
                    Close();
                }
                Connection.Open();
            }
            catch (SqlException e)
            {
                throw;
            }
        }

        public void Close()
        {
            try
            {
                Connection.Close();
            }
            catch (SqlException e)
            {
                
                throw;
            }
        }

        public  void ExcuteNonQuery(string command)
        {
            try
            {
                Command.Connection = Connection;
                Command.CommandType = CommandType.Text;
                Command.CommandText = command;
                Command.ExecuteNonQuery();
            }
            catch(SqlException e)
            {
                throw;
            }
            
        }

        public  object ExcuteScalar(string command)
        {
            try
            {
                Command.Connection = Connection;
                Command.CommandType = CommandType.Text;
                Command.CommandText = command;

                return Command.ExecuteScalar();
            }
            catch (SqlException e)
            {
                throw;
            }

        }

        public  SqlDataReader ExecuteReader(string command)
        {
            try
            {
                Command.Connection = Connection;
                Command.CommandType = CommandType.Text;
                Command.CommandText = command;

                return Command.ExecuteReader();
            }
            catch (SqlException e)
            {
                throw;
            }
        }
    }
}